<style>
    .layui-form-item label {
        width: 120px;
    }
    .layui-form-text{
        margin-bottom: 15px !important;
    }
</style>
<div class="layui-card" style="padding:10px;">
    <div class="layui-tab" lay-filter="setting">
        <ul class="layui-tab-title">
            <li lay-id="base" class="layui-this">基础设置</li>
            <li lay-id="commission">佣金设置</li>
        </ul>
        <div class="layui-tab-content">
            <div class="layui-tab-item layui-show">
                <form class="layui-form seller-alone-form" action="" method="post">
                    <div class="layui-form-item">
                        <label class="layui-form-label">分销层级：</label>
                        <div class="layui-input-block">
                            <input type="radio" name="distribution_level" value="1" title="一级"  {eq name="$data['distribution_level']" value="1" }checked{/eq} >
                            <input type="radio" name="distribution_level" value="2" title="二级" {eq name="$data['distribution_level']" value="2" }checked{/eq}>

                            <input type="radio" name="distribution_level" value="3" title="三级" {eq name="$data['distribution_level']" value="3" }checked{/eq}>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">是否开启店铺：</label>
                        <div class="layui-input-inline seller-inline-12">
                            <input type="radio" name="distribution_store" value="1" title="是"  {eq name="$data['distribution_store']" value="1" }checked{/eq} >
                            <input type="radio" name="distribution_store" value="2" title="否" {eq name="$data['distribution_store']" value="2" }checked{/eq}>
                        </div>
                        <div class="layui-form-mid layui-word-aux">开启后，前台分销商可设置推广自己店铺</div>
                    </div>

                    <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
                        <legend>成为分销商</legend>
                    </fieldset>
                    <div class="layui-form-item">
                        <label class="layui-form-label">成为分销商条件：</label>
                        <div class="layui-input-block">
                            <input type="radio" name="distribution_type" value="1" title="无条件（需要审核）"  {eq name="$data['distribution_type']" value="1" }checked{/eq}>
                            <input type="radio" name="distribution_type" value="2" title="申请（需要审核）" {eq name="$data['distribution_type']" value="2" }checked{/eq}>
                            <input type="radio" name="distribution_type" value="3" title="无需审核"  {eq name="$data['distribution_type']" value="3" }checked{/eq}>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">消费自动成为分销商：</label>
                        <div class="layui-input-inline seller-inline-5">
                            <input type="text" name="distribution_money" placeholder="￥"
                                   value="{$data['distribution_money']}" autocomplete="off" class="layui-input">
                        </div>
                        <div class="layui-form-mid layui-word-aux">
                            元
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label">购买商品成为分销商：</label>
                        <div class="layui-input-block">
                            <input type="radio" name="distribution_goods" lay-filter="distribution_goods" value="1" title="关闭"  {eq name="$data['distribution_goods']" value="1" }checked{/eq} >
                            <input type="radio" name="distribution_goods" lay-filter="distribution_goods" value="2" title="任意商品"  {eq name="$data['distribution_goods']" value="2" }checked{/eq}>
                            <input type="radio" name="distribution_goods" lay-filter="distribution_goods" value="3" title="指定商品"  {eq name="$data['distribution_goods']" value="3" }checked{/eq}>
                        </div>
                    </div>

                    <div class="layui-form-item select-goods" {neq name="$data['distribution_goods']" value="3" } style="display: none;" {/neq} >
                        <label class="layui-form-label">选择商品：</label>
                        <div class="layui-input-block">
                            {Zbshop:sellergoods value="$data.distribution_goods_id" num="1" name="distribution_goods_id" type="checkbox"/}
                        </div>
                    </div>

                    <div class="layui-form-item layui-form-text">
                        <label class="layui-form-label">用户须知：</label>
                        <div class="layui-input-inline seller-inline-9">
                            <textarea placeholder="请输入内容" name="notes" class="layui-textarea">{$data['notes']}</textarea>
                        </div>
                        <div class="layui-form-mid layui-word-aux"></div>
                    </div>

                    <div class="layui-form-item layui-form-text">
                        <label class="layui-form-label">申请协议：</label>
                        <div class="layui-input-inline seller-inline-9">
                            <textarea placeholder="请输入内容" name="agreement"  class="layui-textarea">{$data['agreement']}</textarea>
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label">&nbsp;</label>
                        <div class="layui-input-block">
                            <button class="layui-btn layui-btn-sm" lay-submit="" lay-filter="save" style="height: 37px;padding: 0 27px;font-size: 14px;">保存</button>
                        </div>
                    </div>
                </form>
            </div>

            <div class="layui-tab-item">
                <form class="layui-form seller-alone-form" action="" method="post">
                    <div class="layui-form-item">
                        <label class="layui-form-label">佣金类型：</label>
                        <div class="layui-input-block">
                            <input type="radio"  name="commission_type" value="1" title="百分比"  {eq name="$data['commission_type']" value="1" }checked{/eq} {if condition="!$data['commission_type']"}checked{/if}>
                            <input type="radio"  name="commission_type" value="2" title="固定金额" {eq name="$data['commission_type']" value="2" }checked{/eq}>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">一级佣金：</label>
                        <div class="layui-input-inline seller-inline-5">
                            <input type="number" name="commission_1" value="{$data['commission_1']}" autocomplete="off" class="layui-input" onkeyup="clearNum(this)">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">二级佣金：</label>
                        <div class="layui-input-inline seller-inline-5">
                            <input type="number" name="commission_2"  value="{$data['commission_2']}" autocomplete="off" class="layui-input" onkeyup="clearNum(this)">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">三级佣金：</label>
                        <div class="layui-input-inline seller-inline-5">
                            <input type="text" name="commission_3"  value="{$data['commission_3']}"  autocomplete="off" class="layui-input" onkeyup="clearNum(this)">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">&nbsp;</label>
                        <div class="layui-input-block">
                            <button class="layui-btn layui-btn-sm" lay-submit="" lay-filter="save" style="height: 37px;padding: 0 27px;font-size: 14px;">保存</button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

<script>
    layui.use(['form', 'element'], function() {
        var form = layui.form, element = layui.element;

        //显示当前tab
        if (location.hash) {
            element.tabChange('setting', location.hash.replace(/^#/, ''));
        }
        element.on('tab(setting)', function () {
            var othis = $(this), layid = othis.attr('lay-id');
            if (layid) {
                location.hash = layid;
            }
        });

        //保存商品
        form.on('submit(save)', function(data) {
            formData = data.field;
            if (!formData) {
                layer.msg('请先完善数据', {
                    time: 1300
                });
                return false;
            }
            JsPost('{:get_addon_url("DistributionCenter://Index/setting")}',formData,function(e){
                if (e.status === true) {
                    layer.msg('保存成功');
                } else {
                    layer.msg(e.msg, {
                        time: 1300
                    });
                }
            });
            return false;
        });

        form.on('radio(distribution_goods)', function(data) {
            console.log(data);
            if (data.value == 3) {
                $(".select-goods").show();
            } else {
                $(".select-goods").hide();
            }
        });

    });

    //控制只能输入小数点后2位
    function clearNum(obj) {
        obj.value = obj.value.replace(/[^\d.]/g, "");
        obj.value = obj.value.replace(/\.{2,}/g, ".");
        obj.value = obj.value.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
        obj.value = obj.value.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3');
        if (obj.value.indexOf(".") < 0 && obj.value != "") {
            obj.value = parseFloat(obj.value);
        }
    }
</script>
